﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HelloMinasan.Business;
using HelloMinasan.Business.Extend;
using HelloMinasan.Domain;
using HelloMinasan.Framework.Web;
using HelloMinasan.Framework.Web.WebHelpers;
using HelloMinasan.Resources;
using HelloMinasan.Web.Song.ViewModels;

namespace HelloMinasan.Web.Song.Controllers
{
    public class AdminController : ApplicationController
    {
        private IUserService userService;
        private ISongService songService;

        public AdminController(ISongService songService, IUserService userService)
        {
            this.songService = songService;
            this.userService = userService;
        }

        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult Update()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult Update(int id)
        {
            return View();
        }

        #region Song Control
        [Authorize]
        [HttpGet]
        public ActionResult SongControl()
        {
            var model = SearchSongs(1, "", 0, false);
            return View(model);
        }

        [ValidateInput(false)]
        [Authorize]
        [HttpPost]
        public ActionResult SongListPartial(int? pageNumber, string keyword, int userId, bool isOnlyUnapprove)
        {
            var model = SearchSongs(pageNumber, keyword, userId, isOnlyUnapprove);
            return PartialView(model);
        }

        //private static SongAdminModel SearchSongs(int? pageNumber, string keyword, string username, bool isOnlyUnapprove)
        private SongAdminModel SearchSongs(int? pageNumber, string keyword, int userId, bool isOnlyUnapprove)
        {
            if (pageNumber == null || pageNumber == 0) pageNumber = 1;
            if (string.IsNullOrEmpty(keyword)) keyword = "";
            //if (string.IsNullOrEmpty(username)) username = "";
            //var control = new SongRepository();
            var model = new SongAdminModel()
            {
                ListSong = songService.SearchSong(keyword, pageNumber.Value, isOnlyUnapprove, userId),
                TotalSong = songService.SearchSongCount(keyword, pageNumber.Value, isOnlyUnapprove, userId),
                PagingControl = new PagingHelper() { CurrentPageNumber = pageNumber.Value }
            };
            return model;
        }

        public ActionResult DeleteSong(int pageNumber, string keyword, int userId, int songId, bool isOnlyUnapprove)
        {
            songService.DeleteSong(songId);
            var model = SearchSongs(pageNumber, keyword, userId, isOnlyUnapprove);
            return PartialView("SongListPartial", model);
        }

        public ActionResult Lock(int pageNumber, string keyword, int userId, int songId, bool lockValue, bool isOnlyUnapprove)
        {
            songService.LockSong(songId, lockValue);
            var model = SearchSongs(pageNumber, keyword, userId, isOnlyUnapprove);
            return PartialView("SongListPartial", model);
        }

        [Authorize]
        public ActionResult LyricPartial(int songId)
        {
            //var songRepository = new SongRepository();
            HelloMinasan.Domain.Song song = songService.GetSongContent(songId, true);
            if (song == null)
            {
                return View("AllSongs", songService.GetAllSongs());
            }
            return PartialView(new SongViewModels()
            {
                Song = song,
                Owners = song.User.Username,
                SongName = song.SongName,
                Singer = song.Singer,
                Composer = song.Composer,
                SongId = song.Id
            });
        }

        [Authorize]
        [HttpPost]
        public ActionResult ApproveLyric(int lyricId, bool typeAction)
        {
            //var _songIRepository = new SongRepository();
            ResultObject result = new ResultObject();
            if (Request.IsAuthenticated)
            {
                try
                {
                    songService.ApproveLyric(lyricId, typeAction);
                    result.Result = true;
                    result.Message = Resource.Song_SuccessfullyEvent;
                }
                catch (Exception ex)
                {
                    result.Result = false;
                    result.Message = ex.Message;
                }
            }
            else
            {
                result.Result = false;
                result.Message = Resource.Song_RequiredAuthentication;
            }
            return Json(result);
        }

        #endregion


        #region user
        [Authorize]
        [HttpGet]
        public ActionResult UserControl()
        {
            var model = SearchUsers(1, "");
            return View(model);
        }

        [ValidateInput(false)]
        [Authorize]
        [HttpPost]
        public ActionResult UserListPartial(int? pageNumber, string keyword)
        {
            var model = SearchUsers(pageNumber, keyword);
            return PartialView(model);
        }

        //private static ModelForList<User> SearchUsers(int? pageNumber, string keyword)
        private ModelForList<User> SearchUsers(int? pageNumber, string keyword)
        {
            if (pageNumber == null || pageNumber == 0) pageNumber = 1;
            if (string.IsNullOrEmpty(keyword)) keyword = "";
            //var control = new UserIRepository();
            var model = new ModelForList<User>()
            {
                ListItem = null,//songService.SearchUser(keyword, pageNumber.Value),
                TotalSong = 0,//songService.SearchUserCount(keyword, pageNumber.Value),
                PagingControl = new PagingHelper() { CurrentPageNumber = pageNumber.Value }
            };
            return model;
        }


        public ActionResult LockUser(int pageNumber, string keyword, int userId, bool lockValue, bool isOnlyUnapprove)
        {
            //songService.LockUser(username, lockValue);
            User user = userService.Get(CurrentUser.Id);
            user.Status = UserStatus.Blocked;
            userService.SaveOrUpdate(user);

            var model = SearchSongs(pageNumber, keyword, userId, isOnlyUnapprove);
            return PartialView("UserListPartial", model);
        }
        #endregion
    } 
}
